package com.aaa.aop1.service.aspect;

import com.aaa.aop1.service.annotation.Mypermission;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author:LiWei
 * @date:2022年11月11日15:19
 */
@Component
@Aspect
public class PermissionAdive {

    //切点
    @Pointcut("@annotation(com.aaa.aop1.service.annotation.Mypermission)")
    public void permissionPointcut(){}
    private List<String> permissions=new ArrayList<String>();
    public PermissionAdive(){
        permissions.add("list");
        permissions.add("delete");
        permissions.add("update");
    }
    @Around("permissionPointcut()")
    public Object before(ProceedingJoinPoint joinPoint) throws Throwable {
        //连接点执行方法对象
        MethodSignature methodSignatur= (MethodSignature) joinPoint.getSignature();
        Method method = methodSignatur.getMethod();
        //方法的注解对象
        Mypermission annotation = method.getAnnotation(Mypermission.class);
        if(annotation!=null){
            //获取注解的属性value值
            String value = annotation.value();
            if(!permissions.contains(value)){
                return  "quanxianbuzu";
            }
        }

        return joinPoint.proceed();
    }
}